
import java.awt.Color;
import java.util.ArrayList;

import org.jbox2d.collision.Shape;
import org.jbox2d.common.Vec2;
import org.jbox2d.dynamics.Body;
import org.jbox2d.dynamics.joints.RevoluteJointDef;

import pulpfizz.physics.Actor;
import pulpfizz.physics.BodyUtils;
import pulpfizz.physics.Material;
import pulpfizz.physics.NewWorld;
import pulpfizz.physics.ragdoll.Biped;
import pulpfizz.pulp.body.BodySprite;
import pulpfizz.pulp.body.Java2DBodySprite;
import pulpfizz.pulp.body.PhysicsLayer;

public class PlayerDoll {
	Biped biped;
	NewWorld world;
	FireField field;
	PhysicsLayer physics;
	Body gun;
	Body jetPack;
	PulpFireScene scene;
	boolean isGunUntethered;

	public PlayerDoll(PulpFireScene _scene, ArrayList<Body> tube, boolean leftSide) {
		isGunUntethered = true;
		scene = _scene;
		world = scene.physics.getWorld();
		field = scene.m_field;
		physics = scene.physics;
		Material m = Material.WOOD();
		
		// Create player ragdoll
		if (leftSide) {
			biped = new Biped(world,field.getLeftSemiCenter().add(new Vec2(1f,-0.6f)),2.0f,true,25.0f);
		} else {
			biped = new Biped(world,field.getRightSemiCenter().add(new Vec2(-1f,-0.6f)),2.0f,false,25.0f);
		}
		
		Java2DBodySprite.setFill(Color.WHITE);
		Java2DBodySprite.setStroke(Color.LIGHT_GRAY);
		BodySprite luarm = null;
		BodySprite lhand = null;
		BodySprite llarm = null;
		for (int i=0; i<biped.bodies.length; ++i) {
			Actor a = new Actor();
			a.addToGroup(PulpFireScene.PLAYER_GROUP);
			a.addBody(biped.bodies[i]);
			a.setName("Player");
			BodySprite bodySprite = new Java2DBodySprite(biped.bodies[i], physics,1.0f);
			
			Shape s = biped.bodies[i].getShapeList();
			while (s != null) {
				s.m_groupIndex = -10;
				world.refilter(s);
				s = s.getNext();
			}
			if (biped.bodies[i] == biped.LHand) {
				lhand = bodySprite;
				continue;
			} else if (biped.bodies[i] == biped.LUpperArm) {
				luarm = bodySprite;
				continue;
			} else if (biped.bodies[i] == biped.LForearm) {
				llarm = bodySprite;
				continue;
			}
			physics.add(bodySprite);
		}
		
		float lrSign = leftSide?1.0f:-1.0f;
		// Create jetpack
		jetPack = BodyUtils.createBox(world, biped.Chest.getWorldCenter().x-lrSign*0.7f, 
											 biped.Chest.getWorldCenter().y, 
											 0.75f, 1.3f);
		jetPack.getShapeList().m_groupIndex = -10;
		world.refilter(jetPack.getShapeList());
		m.applyAll(jetPack);
		Java2DBodySprite.setFill(Color.LIGHT_GRAY);
		Java2DBodySprite.setStroke(Color.WHITE);
		physics.add(new Java2DBodySprite(jetPack,physics,1.0f));
		
		scene.jetPackTargetAngle = jetPack.getAngle();
		jetPack.m_angularDamping = 0.1f;
		
		attachToTether(tube, leftSide);
		
		// Create player gun
		
		
		gun = BodyUtils.createBox(world, 
				biped.RHand.getWorldCenter().x+lrSign*.5f,
				biped.RHand.getWorldCenter().y,
				2.0f, 0.3f);
		m.setDensity(20.0f);
		m.applyAll(gun);
		Java2DBodySprite.setFill(Color.BLUE);
		Java2DBodySprite.setStroke(Color.LIGHT_GRAY);
		BodySprite bodySprite = new Java2DBodySprite(gun,physics,1.0f);
		physics.add(bodySprite);
		Shape s = gun.getShapeList();
		while (s != null) {
			s.m_groupIndex = -10;
			world.refilter(s);
			s = s.getNext();
		}

		// Add these last so they show up on top
		physics.add(lhand);
		physics.add(luarm);
		physics.add(llarm);

	}
	
	void attachToTether(ArrayList<Body> tube, boolean leftSide) {
		RevoluteJointDef rjd = new RevoluteJointDef();
		rjd.initialize(jetPack, biped.Chest, jetPack.getWorldCenter().add(new Vec2(0.0f,0.5f)));
		world.createJoint(rjd);
		rjd.initialize(jetPack, biped.Stomach, jetPack.getWorldCenter().add(new Vec2(0.0f,0.0f)));
		world.createJoint(rjd);
	
		rjd.initialize(jetPack, tube.get(tube.size()-1), leftSide?field.getLeftSemiCenter():field.getRightSemiCenter());
		world.createJoint(rjd);
	}
}
